package com.kchouse.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kchouse.MainActivity;
import com.kchouse.MineFragment;
import com.kchouse.R;
import com.kchouse.bean.OrderDetailItem;
import com.kchouse.bean.OrderItem;
import com.kchouse.common.API;
import com.kchouse.common.BaseActivity;
import com.kchouse.common.MyHttpCycleContext;
import com.kchouse.common.ToastUtils;

import java.util.ArrayList;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

public class MyOrderItemActivity extends BaseActivity implements View.OnClickListener{

    private MyOrderItemActivity mContent;
    private TextView tv_title;
    private ImageView iv_back;
    private ListView lv_item;
    private Intent intent;
    private String orderId;
    private ArrayList<OrderDetailItem> listorderDet=new ArrayList();
    private MyOrderAdapter adapter;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order_item);
        mContent = this;
        intiData();
        intiView();
    }

    private void intiView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(title);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);


        lv_item = (ListView) findViewById(R.id.lv_item);

       adapter= new MyOrderAdapter();
        lv_item.setAdapter(adapter);
    }


    private void intiData() {
        intent=getIntent();
        orderId=   intent.getStringExtra("orderId");
        title=   intent.getStringExtra("title");

        GetMineOrderDetails();
    }

    /**
     *  获取我的订单详情
     */
    private void GetMineOrderDetails() {
        String url = API.GetMineOrderDetails+orderId+"/"+ MainActivity.userid;
        RequestParams params = new RequestParams(new MyHttpCycleContext());//请求参数
        params.addHeader("token", MainActivity.token);//添加header信息
//        params.addFormDataPart("page", page);
//        params.addFormDataPart("pageSize", 10);

        HttpRequest.get(url, params, new BaseHttpRequestCallback<String>() {

            //请求网络前
            @Override
            public void onStart() {
                showdialog();
            }

            @Override
            protected void onSuccess(String str) {
                Log.e("==", str);
                JSONObject json = JSONObject.parseObject(str);
                if (json.getBoolean("successful")) {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    listorderDet.addAll(JSONObject.parseArray(data.getString("orderDetail"), OrderDetailItem.class));
                    adapter.notifyDataSetChanged();
                } else {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    if(data.getString("code").equals("401")||data.getString("code").equals("400"))
                    {
                        intent.setClass(mContent, LoginPhoneActivity.class);
                        startActivity(intent);
                        finish();
                    }else {
                        ToastUtils.getInstanc(mContent).showToast(data.getString("msg"));
                    }
                }
            }

            //请求失败（服务返回非法JSON、服务器异常、网络异常）
            @Override
            public void onFailure(int errorCode, String msg) {
//                toast("网络异常~，请检查你的网络是否连接后再试");
                Log.e("==", msg);
            }

            //请求网络结束
            @Override
            public void onFinish() {
                dismissdialog();
            }
        });
    }


    class  MyOrderAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return listorderDet.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           View view= getLayoutInflater().inflate(R.layout.lv_myorder_item,null);

            OrderDetailItem item = listorderDet.get(position);
            TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_name.setText(item.getProjectName());
            TextView tv_total = (TextView) view.findViewById(R.id.tv_total);
            tv_total.setText(item.getTotal()+"");
            TextView tv_use = (TextView) view.findViewById(R.id.tv_use);
            tv_use.setText(item.getUse()+"");
            TextView tv_remain = (TextView) view.findViewById(R.id.tv_remain);
            tv_remain.setText(item.getRemain()+"");
            return view;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
